package com.example.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;

public class EditActivity extends AppCompatActivity {
    private TextView tv_id;
    private EditText edt_s_name;
    private EditText edt_s_age;
    private EditText edt_profession;
    private EditText edt_score;
    private ImageView iv_head;
    private Button btn_edit;
    private Button btnUpdateImg;
    private String path;
    private boolean isClickImgBtn = false;   //是否点击了修改图片的按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        initFindViews();

        initViewData();

        initUpdateImg();

        initBtnEdit();
    }

    private void initBtnEdit() {
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student();
                student.setObjectId(tv_id.getText().toString());
                student.setName(edt_s_name.getText().toString());
                student.setAge(Integer.parseInt(edt_s_age.getText().toString()));
                student.setProfession(edt_profession.getText().toString());
                student.setScore(Integer.parseInt(edt_score.getText().toString()));
                Log.e("EditActivity", "onClick: path = " + path);

                if(isClickImgBtn){
                    student.setHeadimg(new BmobFile(new File(path)));
                }

                Intent intent = new Intent();
                intent.putExtra("STUDENT", student);
                setResult(3002, intent);
                finish();
            }
        });
    }

    //打开手机系统的相册
    private void initUpdateImg() {
        btnUpdateImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 3001);

                isClickImgBtn = true;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 3001 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);  //获取照片路径
            //Log.e("EditActivity", "index = " + columnIndex + " path = " + path );
            cursor.close();

            Bitmap bitmap = BitmapFactory.decodeFile(path);
            iv_head.setImageBitmap(bitmap);
        }
    }

    private void initViewData() {
        Intent intent = getIntent();
        Student student = (Student) intent.getSerializableExtra("STU");

        //将获取的信息编辑到对应的组件中
        tv_id.setText(student.getObjectId());
        edt_s_name.setText(student.getName());
        edt_s_age.setText(String.valueOf(student.getAge()));
        edt_profession.setText(student.getProfession());
        edt_score.setText(String.valueOf(student.getScore()));
        Glide.with(EditActivity.this).load(student.getHeadimg().getUrl()).into(iv_head);
    }

    private void initFindViews() {
        tv_id = findViewById(R.id.tv_id);
        edt_s_name = findViewById(R.id.edt_s_name);
        edt_s_age = findViewById(R.id.edt_s_age);
        edt_profession = findViewById(R.id.edt_profession);
        edt_score = findViewById(R.id.edt_score);
        iv_head = findViewById(R.id.iv_head);
        btn_edit = findViewById(R.id.btn_edit);
        btnUpdateImg = findViewById(R.id.btn_update_img);
    }
}
